<?php

error_reporting(0);

require  './vendor/autoload.php';

/** @var \DI\Container $container */
$container = require './bootstrap/app.php';

/** @var \Asf\Foundation\Server $server */
$server = $container->make(
    \Asf\Foundation\Server::class,
    [
        'address' => my_env('ADDRESS', '0.0.0.0'),
        'port' => my_env('PORT', '8080')
    ]
);

\Asf\Database\ConnectionResolver::$container
    = \Asf\Routing\Route::$container = \Asf\Foundation\Http\Kernel::$container = $container;

\Asf\Foundation\Http\Kernel::staticBootstrap();

require APP_PATH . '/route/web.php';

$server->on('WorkerStart', function($server, $workerId) {
    \Asf\Foundation\Http\Kernel::initDatabase();
});

$server->handleRequest(function($request, $response) use ($container) {
    //把容器放到协程上下文对象中
    \Asf\Util\Context::set('container', $container);

    $kernel = $container->make(
        \Asf\Foundation\Http\Kernel::class
    );

    /** @var \Asf\Contracts\Http\Response $kernelResponse */
    $kernelResponse = $kernel->handle(
        $request = \Asf\Http\Request::createFromSwooleRequest($request)
    );

    $content = $kernelResponse->makeupSwooleResponse($response);

    $response->end($content);

});

$server->run();
